package com.petshop.config;

import com.petshop.interceptor.AdminRefreshTokenInterceptor;
import com.petshop.interceptor.LoginInterceptor;
import com.petshop.interceptor.UserRefreshTokenInterceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //每次用户请求都会刷新redis中token的有效期
        registry.addInterceptor(new AdminRefreshTokenInterceptor(stringRedisTemplate)).addPathPatterns("/**");
        //每次用户请求都会刷新redis中token的有效期
        registry.addInterceptor(new UserRefreshTokenInterceptor(stringRedisTemplate)).addPathPatterns("/**");
        //只进行拦截
        registry.addInterceptor(new LoginInterceptor())
                .excludePathPatterns("/user/login1",
                        "/user/login",
                        "/user/regist",
                        "/user/code",
                        "/admin/login",
                        "/admin/regist",
                        "products/queryProduct",
                        "products/queryFirstProduct",
                        "products/queryProductById",
                        "petGood/queryPetProduct",
                        "petGood/queryPetProductById",
                        "petGood/queryFirstPetProduct");

    }
}
